package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.User;
import com.ylxl.entity.ViewScreen;
import com.ylxl.entity.reponse.ViewScreenVO;
import com.ylxl.entity.request.QueryViewScreen;
import com.ylxl.entity.request.ViewScreenCmdOrMdy;
import com.ylxl.entity.request.ViewScreenStatusMdy;
import com.ylxl.mapper.MateriaProgramMapper;
import com.ylxl.mapper.SysUserMapper;
import com.ylxl.mapper.ViewScreenMapper;
import com.ylxl.service.IViewScreenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户 业务层处理
 *
 */
@Service
public class ViewScreenServiceImpl extends ServiceImpl<ViewScreenMapper, ViewScreen>  implements IViewScreenService {
    private static final Logger log = LoggerFactory.getLogger(ViewScreenServiceImpl.class);

    @Autowired
    private ViewScreenMapper viewScreenMapper;
    @Autowired
    private MateriaProgramMapper materiaProgramMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public List<ViewScreenVO> selectViewScreenList(QueryViewScreen queryViewScreen){
        LambdaQueryWrapper<ViewScreen> viewScreenLambdaQueryWrapper = new LambdaQueryWrapper<>();
        viewScreenLambdaQueryWrapper.like(StringUtils.isNotEmpty(queryViewScreen.getViewId()),ViewScreen::getId,queryViewScreen.getViewId())
                .like(StringUtils.isNotEmpty(queryViewScreen.getViewName()),ViewScreen::getViewName,queryViewScreen.getViewName());
        List<ViewScreen> viewScreens = viewScreenMapper.selectList(viewScreenLambdaQueryWrapper);
        //查询出所有园区信息
        LambdaQueryWrapper<User> gardenLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<User> gardens = userMapper.selectList(gardenLambdaQueryWrapper);
        Map<Integer, User> gardenMap = gardens.stream().collect(Collectors.toMap(User::getId, a -> a, (k1, k2) -> k1));
        List<ViewScreenVO> collect = viewScreens.stream().map(result ->{
                ViewScreenVO view =  new ViewScreenVO();
                BeanUtil.copyProperties(result, view);//属性拷贝
                User garden = gardenMap.get(view.getGardenId());
                return view;
            }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int insertViewScreen(ViewScreenCmdOrMdy viewScreenCmdOrMdy) {
        ViewScreen viewScreen = new ViewScreen();
        BeanUtil.copyProperties(viewScreenCmdOrMdy, viewScreen);//属性拷贝
        return viewScreenMapper.insert(viewScreen);
    }

    @Override
    public int updateViewScreen(ViewScreenCmdOrMdy viewScreenCmdOrMdy) {
        if(StringUtils.isEmpty(viewScreenCmdOrMdy.getId())){
            throw new BaseException("显示屏id不能为空");
        }
        ViewScreen viewScreen = new ViewScreen();
        BeanUtil.copyProperties(viewScreenCmdOrMdy, viewScreen);//属性拷贝
        return viewScreenMapper.updateById(viewScreen);
    }

    @Override
    public ViewScreenVO selctById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("显示屏id不能为空");
        }
        ViewScreenVO viewScreenVO = new ViewScreenVO();
        ViewScreen viewScreen = viewScreenMapper.selectById(id);
        BeanUtil.copyProperties(viewScreen, viewScreenVO);//属性拷贝
        return viewScreenVO;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return viewScreenMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateDisabled(ViewScreenStatusMdy viewScreenStatusMdy) {
        if(StringUtils.isEmpty(viewScreenStatusMdy.getId())||StringUtils.isEmpty(viewScreenStatusMdy.getStatus())){
            throw new BaseException("id或状态不能为空");
        }
        ViewScreen viewScreen = new ViewScreen();
        BeanUtil.copyProperties(viewScreenStatusMdy, viewScreen);//属性拷贝
        return viewScreenMapper.updateById(viewScreen);
    }
}
